﻿using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.LinearGeometry;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.GeometryNET;
using Bentley.Interop.MicroStationDGN;
using CheccCoverplateCulvertAssemble.Common;
using CheccCoverplateCulvertAssemble.Conmmon;
using CheccCoverplateCulvertAssemble.run;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheccCoverplateCulvertAssemble.util
{
    public class SelectLineTool : DgnElementSetTool
    {
        public delegate (Alignment alignment, double bottomElevation, double angle, double disAlong) GetAlignmentLine(bool isSetBingdingData);
        GetAlignmentLine _getAlignmentLine;

        Bentley.DgnPlatformNET.Elements.LineElement element = null;
        public SelectLineTool(GetAlignmentLine getAlignmentLine) : base()
        {
            _getAlignmentLine = getAlignmentLine;
        }

        protected override void OnRestartTool()
        {
            InstallNewInstance();
        }

        protected override void ExitTool()
        {
            if (element != null)
            {
                CommonData.Instance.CommomTotal.SelectLine = element;
                this._getAlignmentLine(true);
            }

            base.ExitTool();
        }

        protected override void OnPostInstall()
        {
            //base.BeginPickElements();
            //开启“精确捕捉”的状态。
            Bentley.DgnPlatformNET.AccuSnap.SnapEnabled = true;
            //开启“精确捕捉”的自动定位功能。
            Bentley.DgnPlatformNET.AccuSnap.LocateEnabled = true;
            //设置捕捉方式为最近点
            Bentley.MstnPlatformNET.Settings.SnapMode = SnapMode.Nearest;
            //base.OnPostInstall();
            NotificationManager.OutputPrompt("选择直线");

            BeginDynamics();

        }

        public void SetSelectElement(Bentley.DgnPlatformNET.Elements.Element el)
        {
            ElementPropertiesSetter eleSetter = new ElementPropertiesSetter();
            eleSetter.SetColor(5);
            // 为元素设置颜色, 并添加到模型种;
            eleSetter.Apply(el);
            el.ReplaceInModel(el);
            element = (Bentley.DgnPlatformNET.Elements.LineElement)el;

            Commands.mainForm.WellDepth();

            ExitTool();
        }

        protected override bool OnDataButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            Bentley.DgnPlatformNET.HitPath hitPath = DoLocate(ev, true, 1);
            if (hitPath == null)
                return false;

            Bentley.DgnPlatformNET.Elements.Element el = hitPath.GetCursorElement();
            if (el == null)
                return false;
            if (el is Bentley.DgnPlatformNET.Elements.LineElement == false)
                return false;
            ElementPropertiesSetter eleSetter = new ElementPropertiesSetter();
            eleSetter.SetColor(5);
            // 为元素设置颜色, 并添加到模型种;
            eleSetter.Apply(el);
            el.ReplaceInModel(el);
            element = (Bentley.DgnPlatformNET.Elements.LineElement)el;

            Commands.mainForm.WellDepth();

            ExitTool();
            return true;
        }

        public override Bentley.DgnPlatformNET.StatusInt OnElementModify(Bentley.DgnPlatformNET.Elements.Element element)
        {
            return Bentley.DgnPlatformNET.StatusInt.Error;
        }

        protected override bool OnResetButton(Bentley.DgnPlatformNET.DgnButtonEvent ev)
        {
            ExitTool();
            return true;
        }


        public void InstallNewInstance()
        {
            InstallTool();
        }


    }
}
